﻿using CP.Utility;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTreeList.StyleFormatConditions;
using GPM;
using LF3.Controllers;
using LF3.Moduls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LF3Proj
{
    public partial class FrmCurrentSample : BaseForm
    {
        private string HeatID;
        private string STNO;
        public FrmCurrentSample()
        {
            InitializeComponent();
        }

        public FrmCurrentSample(string heatid, string stno)
            : this()
        {
            this.HeatID = heatid;
            this.STNO = stno;
        }

        private void FrmCurrentSample_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.HeatID))
                return;
            Lf_Steel_Sample_AnalysisController ctl = new Lf_Steel_Sample_AnalysisController();
            IList<Lf_Steel_Sample_Analysis> samples = ctl.GetLFSample(HeatID);

            LF_CurrentHeatController ctrl = new LF_CurrentHeatController();
            DataTable dt = ctrl.GetHeatStandard(this.STNO);
            if (dt.Rows.Count < 1)
                return;

            try
            {
                Lf_Steel_Sample_Analysis aim = new Lf_Steel_Sample_Analysis();
                aim.al_h = Convert.ToDecimal(dt.Rows[0]["al_aim"]);
                aim.alsol_h = Convert.ToDecimal(dt.Rows[0]["alsol_aim"]);
                aim.as_h = Convert.ToDecimal(dt.Rows[0]["as_aim"]);
                aim.b_h = Convert.ToDecimal(dt.Rows[0]["b_aim"]);
                aim.c_h = Convert.ToDecimal(dt.Rows[0]["c_aim"]);
                aim.ca_h = Convert.ToDecimal(dt.Rows[0]["ca_aim"]);
                aim.ce_h = Convert.ToDecimal(dt.Rows[0]["ce_aim"]);
                aim.co_h = Convert.ToDecimal(dt.Rows[0]["co_aim"]);
                aim.cr_h = Convert.ToDecimal(dt.Rows[0]["cr_aim"]);
                aim.cu_h = Convert.ToDecimal(dt.Rows[0]["cu_aim"]);
                aim.h_h = Convert.ToDecimal(dt.Rows[0]["h_aim"]);
                aim.mg_h = Convert.ToDecimal(dt.Rows[0]["mg_aim"]);
                aim.mn_h = Convert.ToDecimal(dt.Rows[0]["mn_aim"]);
                aim.mo_h = Convert.ToDecimal(dt.Rows[0]["mo_aim"]);
                aim.n_h = Convert.ToDecimal(dt.Rows[0]["n_aim"]);
                aim.nb_h = Convert.ToDecimal(dt.Rows[0]["nb_aim"]);
                aim.ni_h = Convert.ToDecimal(dt.Rows[0]["ni_aim"]);
                aim.o_h = Convert.ToDecimal(dt.Rows[0]["o_aim"]);
                aim.p_h = Convert.ToDecimal(dt.Rows[0]["p_aim"]);
                aim.pb_h = Convert.ToDecimal(dt.Rows[0]["pb_aim"]);
                aim.s_h = Convert.ToDecimal(dt.Rows[0]["s_aim"]);
                aim.sb_h = Convert.ToDecimal(dt.Rows[0]["sb_aim"]);
                aim.si_h = Convert.ToDecimal(dt.Rows[0]["si_aim"]);
                aim.sn_h = Convert.ToDecimal(dt.Rows[0]["sn_aim"]);
                aim.ti_h = Convert.ToDecimal(dt.Rows[0]["ti_aim"]);
                aim.v_h = Convert.ToDecimal(dt.Rows[0]["v_aim"]);
                aim.w_h = Convert.ToDecimal(dt.Rows[0]["w_aim"]);
                aim.zr_h = Convert.ToDecimal(dt.Rows[0]["zr_aim"]);
                aim.sampleid = "内控目标";
                samples.Insert(0, aim);

                Lf_Steel_Sample_Analysis high = new Lf_Steel_Sample_Analysis();
                high.al_h = Convert.ToDecimal(dt.Rows[0]["al_max"]);
                high.alsol_h = Convert.ToDecimal(dt.Rows[0]["alsol_max"]);
                high.as_h = Convert.ToDecimal(dt.Rows[0]["as_max"]);
                high.b_h = Convert.ToDecimal(dt.Rows[0]["b_max"]);
                high.c_h = Convert.ToDecimal(dt.Rows[0]["c_max"]);
                high.ca_h = Convert.ToDecimal(dt.Rows[0]["ca_max"]);
                high.ce_h = Convert.ToDecimal(dt.Rows[0]["ce_max"]);
                high.co_h = Convert.ToDecimal(dt.Rows[0]["co_max"]);
                high.cr_h = Convert.ToDecimal(dt.Rows[0]["cr_max"]);
                high.cu_h = Convert.ToDecimal(dt.Rows[0]["cu_max"]);
                high.h_h = Convert.ToDecimal(dt.Rows[0]["h_max"]);
                high.mg_h = Convert.ToDecimal(dt.Rows[0]["mg_max"]); ;
                high.mn_h = Convert.ToDecimal(dt.Rows[0]["mn_max"]);
                high.mo_h = Convert.ToDecimal(dt.Rows[0]["mo_max"]);
                high.n_h = Convert.ToDecimal(dt.Rows[0]["n_max"]);
                high.nb_h = Convert.ToDecimal(dt.Rows[0]["nb_max"]);
                high.ni_h = Convert.ToDecimal(dt.Rows[0]["ni_max"]);
                high.o_h = Convert.ToDecimal(dt.Rows[0]["o_max"]);
                high.p_h = Convert.ToDecimal(dt.Rows[0]["p_max"]);
                high.pb_h = Convert.ToDecimal(dt.Rows[0]["pb_max"]);
                high.s_h = Convert.ToDecimal(dt.Rows[0]["s_max"]);
                high.sb_h = Convert.ToDecimal(dt.Rows[0]["sb_max"]);
                high.si_h = Convert.ToDecimal(dt.Rows[0]["si_max"]);
                high.sn_h = Convert.ToDecimal(dt.Rows[0]["sn_max"]);
                high.ti_h = Convert.ToDecimal(dt.Rows[0]["ti_max"]);
                high.v_h = Convert.ToDecimal(dt.Rows[0]["v_max"]);
                high.w_h = Convert.ToDecimal(dt.Rows[0]["w_max"]);
                high.zr_h = Convert.ToDecimal(dt.Rows[0]["zr_max"]);
                high.sampleid = "内控上限";
                samples.Insert(0, high);

                Lf_Steel_Sample_Analysis low = new Lf_Steel_Sample_Analysis();
                low.al_h = Convert.ToDecimal(dt.Rows[0]["al_min"]);
                low.alsol_h = Convert.ToDecimal(dt.Rows[0]["alsol_min"]);
                low.as_h = Convert.ToDecimal(dt.Rows[0]["as_min"]);
                low.b_h = Convert.ToDecimal(dt.Rows[0]["b_min"]);
                low.c_h = Convert.ToDecimal(dt.Rows[0]["c_min"]);
                low.ca_h = Convert.ToDecimal(dt.Rows[0]["ca_min"]);
                low.ce_h = Convert.ToDecimal(dt.Rows[0]["ce_min"]);
                low.co_h = Convert.ToDecimal(dt.Rows[0]["co_min"]);
                low.cr_h = Convert.ToDecimal(dt.Rows[0]["cr_min"]);
                low.cu_h = Convert.ToDecimal(dt.Rows[0]["cu_min"]);
                low.h_h = Convert.ToDecimal(dt.Rows[0]["h_min"]);
                low.mg_h = Convert.ToDecimal(dt.Rows[0]["mg_min"]); ;
                low.mn_h = Convert.ToDecimal(dt.Rows[0]["mn_min"]);
                low.mo_h = Convert.ToDecimal(dt.Rows[0]["mo_min"]);
                low.n_h = Convert.ToDecimal(dt.Rows[0]["n_min"]);
                low.nb_h = Convert.ToDecimal(dt.Rows[0]["nb_min"]);
                low.ni_h = Convert.ToDecimal(dt.Rows[0]["ni_min"]);
                low.o_h = Convert.ToDecimal(dt.Rows[0]["o_min"]);
                low.p_h = Convert.ToDecimal(dt.Rows[0]["p_min"]);
                low.pb_h = Convert.ToDecimal(dt.Rows[0]["pb_min"]);
                low.s_h = Convert.ToDecimal(dt.Rows[0]["s_min"]);
                low.sb_h = Convert.ToDecimal(dt.Rows[0]["sb_min"]);
                low.si_h = Convert.ToDecimal(dt.Rows[0]["si_min"]);
                low.sn_h = Convert.ToDecimal(dt.Rows[0]["sn_min"]);
                low.ti_h = Convert.ToDecimal(dt.Rows[0]["ti_min"]);
                low.v_h = Convert.ToDecimal(dt.Rows[0]["v_min"]);
                low.w_h = Convert.ToDecimal(dt.Rows[0]["w_min"]);
                low.zr_h = Convert.ToDecimal(dt.Rows[0]["zr_min"]);
                low.sampleid = "内控下限";
                samples.Insert(0, low);


                this.gcLFSample.DataSource = samples;
                this.gridControl1.DataSource = samples;
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message);
            }

        }

        private void gvLFSample_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //object o = gvLFSample.GetDataRow(e.RowHandle)[e.Column.AbsoluteIndex - 1];
        }

        private void gvLFSample_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            try
            {
                GridView View = sender as GridView;
                LF_CurrentHeatController ctrl = new LF_CurrentHeatController();
                DataTable dt = ctrl.GetHeatStandard(this.STNO);
                if (dt.Rows.Count < 1) return;
                if (e.RowHandle >= 0 && e.RowHandle < 3)
                {
                    e.Appearance.BackColor = Color.FromArgb(135, 206, 250);
                }

                if (e.RowHandle > 2)
                {
                    if (e.Column.FieldName.Contains("_h") && e.Column.FieldName != "zn_h" && e.Column.FieldName != "bi_h"
                        && e.Column.FieldName != "bso1_h" && e.Column.FieldName != "re_h"
                        && e.Column.FieldName != "di_h" && e.Column.FieldName != "ceq_h"
                         && e.Column.FieldName != "mnq_h")
                    {
                        string colname = e.Column.FieldName.Substring(0, e.Column.FieldName.IndexOf("_h"));
                        object oValue = View.GetRowCellValue(e.RowHandle, View.Columns[e.Column.FieldName]);
                        if (oValue != null)
                        {
                            double value = Convert.ToDouble(oValue);
                            double min = dt.Rows[0][colname + "_min"] == DBNull.Value ? 0 : Convert.ToDouble(dt.Rows[0][colname + "_min"]);
                            double max = dt.Rows[0][colname + "_max"] == DBNull.Value ? 0 : Convert.ToDouble(dt.Rows[0][colname + "_max"]);
                            if (value > max)
                            {
                                e.Appearance.BackColor = Color.Red;
                                // e.Appearance.BackColor2 = Color.LightCyan;
                            }

                            if (value < min)
                            {
                                e.Appearance.BackColor = Color.FromArgb(254, 154, 0);
                                // e.Appearance.BackColor2 = Color.LightCyan;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message);
            }
        }

        private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            try
            {
                GridView View = sender as GridView;
                LF_CurrentHeatController ctrl = new LF_CurrentHeatController();
                DataTable dt = ctrl.GetHeatStandard(this.STNO);
                if (dt.Rows.Count < 1) return;
                if (e.RowHandle >= 0 && e.RowHandle < 3)
                {
                    e.Appearance.BackColor = Color.FromArgb(135, 206, 250);
                }

                if (e.RowHandle > 2)
                {
                    if (e.Column.FieldName.Contains("_h") && e.Column.FieldName != "zn_h" && e.Column.FieldName != "bi_h"
                        && e.Column.FieldName != "bso1_h" && e.Column.FieldName != "re_h"
                        && e.Column.FieldName != "di_h" && e.Column.FieldName != "ceq_h"
                         && e.Column.FieldName != "mnq_h")
                    {
                        string colname = e.Column.FieldName.Substring(0, e.Column.FieldName.IndexOf("_h"));
                        object oValue = View.GetRowCellValue(e.RowHandle, View.Columns[e.Column.FieldName]);
                        if (oValue != null)
                        {
                            double value = Convert.ToDouble(oValue);
                            double min = dt.Rows[0][colname + "_min"] == DBNull.Value ? 0 : Convert.ToDouble(dt.Rows[0][colname + "_min"]);
                            double max = dt.Rows[0][colname + "_max"] == DBNull.Value ? 0 : Convert.ToDouble(dt.Rows[0][colname + "_max"]);
                            if (value > max)
                            {
                                e.Appearance.BackColor = Color.Red;
                                // e.Appearance.BackColor2 = Color.LightCyan;
                            }

                            if (value < min)
                            {
                                e.Appearance.BackColor = Color.FromArgb(254, 154, 0);
                                // e.Appearance.BackColor2 = Color.LightCyan;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message);
            }
        }


    }
}
